package xyz.eden.gateway.springcloud.admin.protocol.iplist;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IpClientServiceInstanceProperties {
    /**
     * 服务 id
     */
    private String serviceId;
    /**
     * uri
     */
    private List<URI> uriList;

    public List<IpClientServiceInstance> builderServiceInstance() {
        return Optional.ofNullable(this.uriList).orElse(Collections.emptyList())
                .stream().map(uri -> {
                    return IpClientServiceInstance.builder()
                            .serviceId(this.serviceId)
                            .host(uri.getHost())
                            .port(uri.getPort())
                            .secure(Objects.equals(uri.getScheme(), "https"))
                            .build();
                }).collect(Collectors.toList());

    }
}
